package com.shop.shard.controller.rest.product;

import com.shop.shard.common.*;
import com.shop.shard.controller.assembler.product.ProductAssembler;
import com.shop.shard.controller.command.product.ProductCreateCommand;
import com.shop.shard.controller.command.product.ProductUpdateCommand;
import com.shop.shard.entity.product.Product;
import com.shop.shard.vo.product.ProductVO;
import com.shop.shard.service.product.ProductService;
import com.shop.shard.manager.product.ProductManager;
import com.shop.shard.mapper.product.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * @Description: 代码生成器自动生成
 * @author:
 * @Createed Date: 2019-8-16 10:06:57
 * @ModificationHistory: Who  When  What
 * ---------     -------------   --------------------------------------
 **/
@RestController
@RequestMapping("api/v1/product/product")
@Api(value = "productService", description = "product相关AIP")
public class ProductRest  {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductManager productManager;

    @Value("${snow.work.id}")
    private String value;

    @PostMapping("add")
    @ApiOperation(value = "add Product ", notes = "xxx")
    public InvokeResult add(@RequestBody @Valid ProductCreateCommand command) {
        Product entity = ProductAssembler.toProduct(command);
        productService.addEntity(entity);
        return InvokeResult.success();
    }

    /**
    * delect Product by id
    * @return
    */
    @PostMapping("delete")
    @ApiOperation(value = "delect Product by id", notes = "xxx")
    public InvokeResult delete(@RequestBody CommonDTO commonDTO){
        productService.delete(commonDTO.getIds());
        return InvokeResult.success();
    }


    @PostMapping("update")
    @ApiOperation(value = "update Product by id", notes = "xxx")
    public InvokeResult update(@RequestBody @Valid ProductUpdateCommand command) {
        Product entity = ProductAssembler.toProduct(command);
        productService.updateEntity(entity);
        return InvokeResult.success();
    }

    @GetMapping("detail")
    @ApiOperation(value = "get Product detail by id", notes = "xxx")
    public InvokeResult detail(@RequestParam() Integer id) {
        ProductVO vo = productManager.selectDetailVoById(id);
        return InvokeResult.success(vo);
    }

    @GetMapping("page")
    @ApiOperation(value = "get Product page ", notes = "xxx")
    public IPage<ProductVO> page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10")Integer pageSize){
        Page page = new Page(pageNum,pageSize);
        Map<String,Object> map=new HashMap<>(4);
        IPage<ProductVO> iPage=  productManager.getPage(page,map);
        return iPage;
    }
}
